/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package cn.eclass.dao;

import cn.eclass.entity.AdminEntity;
import cn.eclass.entity.Entity;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author fortruth
 */
public class AdminDao extends CommonDao {

    private String TABLE_NAME = "admin";

    public AdminDao() {
        super(AdminDao.class.getName());
    }

    @Override
    protected Entity fillEntity(ResultSet resultSet) {
        AdminEntity adminEntity = new AdminEntity();
        try {
            adminEntity.setId(resultSet.getInt("ID"));
            adminEntity.setUsername((resultSet.getString("USERNAME")));
            adminEntity.setPassword(resultSet.getString("PASSWORD"));
            adminEntity.setEmail(resultSet.getString("EMAIL"));
        } catch (SQLException ex) {
            logger.error(ex);
        }
        return adminEntity;
    }

    public List<AdminEntity> list() {
        String sql = "select * from " + TABLE_NAME;
        return (List<AdminEntity>) super.select(sql);
    }

    public AdminEntity get(int id) {
        String sql = "select * from admin where id = " + id;
        List<AdminEntity> admins = (List<AdminEntity>) super.select(sql);
        return admins.get(0);
    }

    public int add(AdminEntity admin) {
        String sql = "insert into admin "
                + "(`username`, `email`) "
                + " values "
                + "('" + admin.getUsername() + "','" + admin.getEmail() + "')";
        return super.insert(sql);
    }

    public boolean update(AdminEntity user) {
        String sql = "update admin set "
                + "`username` = '" + user.getUsername() + "',"
                + "`password` = '" + user.getPassword() + "',"
                + "`email` = '" + user.getEmail() + "'"
                + " where id = " + user.getId();
        int effectedRows = super.update(sql);
        if (effectedRows > 0) {
            return true;
        } else {
            return false;
        }
    }

    public boolean delete(int id) {
        String sql = "delete from admin where id = " + id;
        int effectRows = super.delete(sql);
        if (effectRows > 0) {
            return true;
        } else {
            return false;
        }
    }
}
